package com.michael.demos.springboot.mybatisplus.customizedId.core;

import cn.hutool.core.lang.Snowflake;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * ID生成器
 * 套用 hutool Snowflake
 */
public class IdMaker implements Serializable {
    private static final long serialVersionUID = 1L;

    private final Snowflake snowflake;

    private static final Date DEFAULT_START_POINT = new GregorianCalendar(2021, Calendar.JUNE, 20, 13, 14, 0).getTime();

    public IdMaker(Date startPoint, long workerId, long dataCenterId, boolean isUseSystemClock) {
        startPoint = startPoint != null ? startPoint : DEFAULT_START_POINT;
        this.snowflake = new Snowflake(startPoint, workerId, dataCenterId, isUseSystemClock);
    }

    public IdMaker(long workerId, long dataCenterId, boolean isUseSystemClock) {
        this.snowflake = new Snowflake(DEFAULT_START_POINT, workerId, dataCenterId, isUseSystemClock);
    }

    public long next() {
        return snowflake.nextId();
    }
}
